//
//  XMeHeaderView.m
//  ReceiveVouchers
//
//  Created by admin on 2017/7/10.
//  Copyright © 2017年 ReceiveVouchers. All rights reserved.
//

#import "XMeHeaderView.h"

#import "XCardPackageButton.h"
#import "UIImage+VR.h"
#import "NSString+Tools.h"
#import "KAUserinfoModel.h"


@interface XMeHeaderView()

@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UIImageView *iconImgView;

@property (nonatomic, strong) UIButton *memberBtn;
@property (nonatomic, weak) UIButton *clockBtn;

@property (nonatomic, strong) NSMutableArray *cardPackageBtnArray;


@end

@implementation XMeHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.cardPackageBtnArray = [NSMutableArray array];
        [self setupSubView];
    }
    
    return self;
}

- (void)setupSubView
{
    
//    self.backgroundColor = MAINColor;
    
    UIImageView *bgImageView = [[UIImageView alloc] init];
    bgImageView.backgroundColor = MAINColor;
    bgImageView.frame = CGRectMake(0, 0, kScreenWidth, 216 + STATUS_BAR_HEIGHT - 20);
    [self addSubview:bgImageView];
    self.bgImageView = bgImageView;

    // 头像
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.userInteractionEnabled = YES;
    iconImgView.image = [UIImage imageNamed:@"def_avatar"];
    iconImgView.contentMode = UIViewContentModeScaleAspectFill;
    iconImgView.layer.cornerRadius = 30;
    iconImgView.layer.masksToBounds = YES;
    [self addSubview:iconImgView];
    self.iconImgView = iconImgView;
    [iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.centerX.mas_equalTo(self);
//        make.centerY.mas_equalTo(self).offset(STATUS_BAR_HEIGHT - 20);
        make.width.height.mas_equalTo(60);
    }];
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapGesture:)];
    [iconImgView addGestureRecognizer:tapGesture];
    
    // 手机号
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"潘志文";
    nameLabel.font = VRFont(18);
    nameLabel.textColor = RGB(51, 51, 51);
    [self addSubview:nameLabel];
    self.nameLabel = nameLabel;
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(iconImgView);
        make.top.mas_equalTo(iconImgView.mas_bottom).offset(9);
    }];
    
    UIView *bottomView = [[UIView alloc] init];
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(68);
    }];
    
    // 打卡按钮
    UIButton *clockBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    clockBtn.adjustsImageWhenDisabled = NO;
    clockBtn.adjustsImageWhenHighlighted = NO;
    clockBtn.backgroundColor = [UIColor whiteColor];
    [clockBtn setTitle:@"打卡" forState:UIControlStateNormal];
    [clockBtn setTitle:@"已打卡" forState:UIControlStateDisabled];
    clockBtn.titleLabel.font = VRFont(13);
    [clockBtn setTitleColor:RGB(108, 108, 108) forState:UIControlStateNormal];
    [clockBtn setImage:[UIImage imageNamed:@"me_punch_card"] forState:UIControlStateNormal];
    [clockBtn setImage:[[UIImage alloc] init] forState:UIControlStateDisabled];
    [clockBtn setImage:nil forState:UIControlStateSelected];
    clockBtn.layer.cornerRadius = 14;
    clockBtn.layer.masksToBounds = YES;
    [clockBtn addTarget:self action:@selector(clockBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:clockBtn];
    self.clockBtn = clockBtn;
    [clockBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(STATUS_BAR_HEIGHT + 8);
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(56);
        make.height.mas_equalTo(28);
    }];
    
    [self setupCardPackageViewWith:bottomView];
}


- (void)setupCardPackageViewWith:(UIView *)bottomView
{
    
    CGFloat buttonW = (kScreenWidth - 1) / 3;

    NSArray *cardPackageTitleArray = @[@"已连续打卡", @"记账总天数", @"记账总笔数"];
    for (int i = 0; i < 3; i++) {
        
        XCardPackageButton *cardPackageBtn = [XCardPackageButton buttonWithType:UIButtonTypeCustom];
        cardPackageBtn.tag = i;
        cardPackageBtn.titleLbl.text = cardPackageTitleArray[i];
        cardPackageBtn.countLbl.text = @"64";
        
        [cardPackageBtn addTarget:self action:@selector(cardPackageAction:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:cardPackageBtn];
        [cardPackageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(i * buttonW);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(bottomView);
            make.width.mas_equalTo(buttonW);
        }];
        
        [self.cardPackageBtnArray addObject:cardPackageBtn];
    }
}


#pragma mark - action
- (void)cardPackageAction:(UIButton *)sender
{
    if (self.CardPackageActionOperation) {
        
        self.CardPackageActionOperation(sender.tag);
    }
}

- (void)onTapGesture:(UITapGestureRecognizer *)recongnizer
{
    if (self.HeaderActionOperation) {
        self.HeaderActionOperation();
    }
}

- (void)clockBtnClick:(UIButton *)sender
{
    sender.enabled = NO;
    
    if (self.clockBlock) {
        self.clockBlock();
    }
}


#pragma mark - Tool
- (NSString *)decodeFromPercentEscapeString: (NSString *) urlStr
{
    NSMutableString *outputStr = [NSMutableString stringWithString:urlStr];
    [outputStr replaceOccurrencesOfString:@"+"
                               withString:@""
                                  options:NSLiteralSearch
                                    range:NSMakeRange(0,
                                                      [outputStr length])];
    
    return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}


#pragma mark - setter && getter
- (void)setUserinfoModel:(KAUserinfoModel *)userinfoModel
{
    _userinfoModel = userinfoModel;
    
    if (userinfoModel.image.length > 0) {
        
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        path = [path stringByAppendingFormat:@"/%@", userinfoModel.image];
        self.iconImgView.image = [UIImage imageWithContentsOfFile:path];;
    }else{
        self.iconImgView.image = [UIImage imageNamed:@"def_avatar"];
    }
    
    self.nameLabel.text = userinfoModel.name.length > 0 ? userinfoModel.name : @"匿名用户";
    
    ((XCardPackageButton *)self.cardPackageBtnArray[0]).countLbl.text = [NSString stringWithFormat:@"%ld", (long)userinfoModel.clockCount];
    ((XCardPackageButton *)self.cardPackageBtnArray[2]).countLbl.text = [NSString stringWithFormat:@"%ld", (long)userinfoModel.accountCount];
    // 计算天数
    NSInteger dayCount = 0;
    if (userinfoModel.firstaccountDate.length > 0) {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSDate *firstAccountDate = [formatter dateFromString:userinfoModel.firstaccountDate];
        NSInteger secondDif = [firstAccountDate timeIntervalSinceNow];
        
        dayCount = ABS(floorf(secondDif * 1.0 / (24 * 60 * 60)));
    }
    ((XCardPackageButton *)self.cardPackageBtnArray[1]).countLbl.text = [NSString stringWithFormat:@"%ld", (long)dayCount];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *today = [formatter stringFromDate:[NSDate date]];
    if ([userinfoModel.lastClockDate isEqualToString:today]) {
        
        self.clockBtn.enabled = NO;
    }
    
}

@end
